<template>
     <el-button type="primary" id="mainbutton" @click="checkwhite" @keyup.enter="checkwhite">&nbsp;&nbsp;&nbsp;生成二维码&nbsp;&nbsp;&nbsp;</el-button>
</template>

<script>
import ElementPlus from 'element-plus'
import axios from 'axios'
import 'element-plus/lib/theme-chalk/index.css'
export default {
name:'Subbutton',
data(){
  return{
    qrtxt:'',
    id:'',
    item:{
      name:'',
      img:'',
      time:'',
    }
}
},
mounted:function(){
    document.addEventListener('keydown', (e) => {
	let key = window.event.keyCode;
    if (key == 13) {
       this.checkwhite();
	}
})
},
methods:{
  sendmes(){
  console.log("send")
  let that=this;
 axios({
    headers:{'Content-Type': "application/json;charset=UTF-8"},
     responseType: 'arraybuffer',
    //  headers:{'Content-Type': "multipart/form-data"},
    url:'http://6rm52f.natappfree.cc/CreateQR/SimpleQR',
    method:'post',
    data:{
        size:this.$store.state.size,
        content:this.qrtxt+'',
    }
}).then(res=>{
 res= 'data:image/png;base64,' + btoa(
          new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), '')
        )

    console.log(res);
      let mu = res;
      that.$store.commit('geturl',mu);
      let data={};
        let txt=document.getElementById('textboot').value;
        txt=txt.length<18?txt:txt.slice(0,17)+'...'
        data.name=txt;
        data.img= mu;
        data.time=this.gettime();
        that.$store.state.history.push(data);
         that.$router.push({path:'/Sucbox',query:{id:that.qrtxt,src:0}})
        // console.log(that.$store.state.history)
 
      })
    .catch(err=>{
      that.$message.error({
          message: '宝啊，好像有些错误了呢'})
      console.log(err)
      });

     },
checkwhite(){
 this.qrtxt=document.getElementById('textboot').value;
  document.getElementById("textboot").style.border='none'
  if(this.qrtxt==''){
     document.getElementById("textboot").style.border=' dashed 1.5px red'
      this.$message({
          message: '小可爱，输入不能为空哦',
          type: 'warning'
        });
  }
  else{
    this.sendmes();       
  }

},
gettime(){
let d = new Date();

let hour= d.getHours();//得到小时数

let min= d.getMinutes();//得到分钟数

if(hour<10){
  hour='0'+hour+'';
}
if(min<10){
  min='0'+min+'';
}
return hour+':'+min+'';
}

}

}
</script>

<style >
#mainbutton{
  position: absolute;
top:450px;
left:600px;
 transform: 2s;
}

</style>

 